* Code to reproduce the continuous part  of “Online Appendix C. Uncorrelated longitudinal TPM for the effect of fiscal decentralization on organized crime violence (2010-2015) using “Dez_and_Death_integrated_dataset.dta”

xtset code year
keep if year==2010| year ==2015
gen dum_2010 = 1 if year ==2010
replace dum_2010=0 if dum_2010==.
gen dum_2015 = 1 if year ==2015
replace dum_2015=0 if dum_2015==.
xtset code year
sort code year
tabulate Estado_ID, gen(Estado)

* Variables related to security spending per capita and the local police pear capita are multiply imputed with 50 datased (M=50)
mi set mlong
mi xtset code year
mi register imputed Sec_Spending_Cap_Lag_1015 Loc_Police_Per_Head_Ipol
*faktorvariablen koennen nicht in die Imputationsstufe aufgenommen werden, deshalb dummies separat. 
mi register regular Homicides_All_Cap_Pos_Log  mn_Spending_Cap_Real_Munic dev_Spending_Cap_Real_Munic   mn_Own_Revenue_Share dev_Own_Revenue_Share   gini poverty Share_Head_of_HH_Female Household_Size Population_Density  Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast dum_2015 Estado1-Estado32 
mi impute mvn Sec_Spending_Cap_Lag_1015 Loc_Police_Per_Head_Ipol = Homicides_All_Cap_Pos_Log  mn_Spending_Cap_Real_Munic dev_Spending_Cap_Real_Munic   mn_Own_Revenue_Share dev_Own_Revenue_Share   gini poverty Share_Head_of_HH_Female Household_Size Population_Density Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast dum_2015 Estado1-Estado32, add(50) force
mi xeq 0 1 20 50: summarize Sec_Spending_Cap_Lag_1015 Loc_Police_Per_Head_Ipol
mi xtset code year
mi estimate: xtreg Homicides_All_Cap_Pos_Log  mn_Spending_Cap_Real_Munic dev_Spending_Cap_Real_Munic   mn_Own_Revenue_Share dev_Own_Revenue_Share   Sec_Spending_Cap_Lag_1015 Loc_Police_Per_Head_Ipol gini poverty Share_Head_of_HH_Female Household_Size Population_Density Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast dum_2015 Estado1-Estado32,  re robust

*Results and important additional parameters are stored in an excel file
putexcel set "resultsPanel1015_hyb.xlsx", sheet("OLS_1015_MI_Hyb") modify
putexcel I3=(" Number of obs")
putexcel J3= (e(N_mi))
putexcel I4=("Number of groups")
putexcel J4=( e(N_g_mi))
putexcel I5=("F_mi")
putexcel J5=( e(F_mi))
matrix a = r(table)'
matrix a = a[., 1..6]
putexcel A2=matrix(a)

*combining pseudo R2 for all M
mi query
local M=50
scalar r2_o = 0
 qui mi xeq 1/`M': xtreg Homicides_All_Cap_Pos_Log  mn_Spending_Cap_Real_Munic dev_Spending_Cap_Real_Munic   mn_Own_Revenue_Share dev_Own_Revenue_Share    Sec_Spending_Cap_Lag_1015 Loc_Police_Per_Head_Ipol gini poverty Share_Head_of_HH_Female Household_Size Population_Density Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast dum_2015 Estado1-Estado32, re robust; scalar r2_o = r2_o + atanh(sqrt(e(r2_o)))

scalar r2_o = tanh(r2_o/`M')^2
di as txt " R2 using Fisher's z over imputed data = " as res r2_o

*storing results in excel sheet
putexcel set "resultsPanel1015_hyb.xlsx", sheet("OLS_1015_MI_Hyb") modify
putexcel I6=(" R2 using Fisher's z over imputed data ")
putexcel J6= (e(r2_o))
